//
//  WDWorderOrderWaitVehicleViewController.m
//  wd-serviceapp
//
//  Created by Bin Tang on 2024/8/5.
//

#import "WDWorderOrderWaitVehicleViewController.h"
#import "WDWorderOrderWaitVehicleViewModel.h"
#import "WDWorderOrderWaitVehicleTopView.h"
#import "WDWorderOrderWaitVehicleBottomView.h"

@interface WDWorderOrderWaitVehicleViewController ()
@property (nonatomic, strong) WDWorderOrderWaitVehicleViewModel *viewModel;

@property (nonatomic, strong) WDWorderOrderWaitVehicleTopView *topView;


@property (nonatomic, strong) WDWorderOrderWaitVehicleBottomView *bottomView;

@end

@implementation WDWorderOrderWaitVehicleViewController

- (void)dealloc {
    debugMethod();
}

- (void)initWithVar {
    [super initWithVar];
    
}

- (void)initWithViewModel {
    _viewModel = [WDWorderOrderWaitVehicleViewModel new];
    _viewModel.serviceModel = self.serviceModel;
    [_viewModel setupData];
}

- (void)initWithNavBar {
    [super initWithNavBar];
    
    [self initWithBackButton];
    
    self.navigationItem.title = self.viewModel.title;
    
}

- (void)initWithSubView {
    [super initWithSubView];
    
    [self initWithRootContent:NO];
    self.rootContent.height = self.rootContent.height - TABBAR_HEIGHT;
    
    [self initWithTopView];
    
    [self initWithBottomView];
}

- (void)initWithTopView {
    if (!_topView) {
        CGRect frame = CGRectMake(0, 0, kScreenW, kWDWorderOrderWaitVehicleTopViewHeight);
        _topView = [[WDWorderOrderWaitVehicleTopView alloc] initWithFrame:frame];
        
        _topView.hiddenApplyCancel = ![[EDSAppDelegateTool sharedInstance] hasPermission:@(10010103) parentCode:@(100101)];
        
        [self.rootContent addSubview:_topView];
        
        @weakify(self);
        _topView.touchDetailBlock = ^{
            @strongify(self);
            [self actionWithDetail];
        };
        _topView.touchApplyCancelBlock = ^{
            @strongify(self);
            [self actionWithApplyCancel:nil];
        };
    }
    [_topView updateWithViewData:self.viewModel.serviceModel];
}

- (void)initWithBottomView {
    
    if (![[EDSAppDelegateTool sharedInstance] hasPermission:@(10010102) parentCode:@(100101)]) {
        return;
    }
    
    if (!_bottomView) {
        CGRect frame = CGRectMake(0, self.rootContent.bottom, kScreenW, TABBAR_HEIGHT);
        _bottomView = [[WDWorderOrderWaitVehicleBottomView alloc] initWithFrame:frame];
        [self.view addSubview:_bottomView];
        
        @weakify(self);
        _bottomView.touchScanCodeBlock = ^{
            @strongify(self);
            [self actionWithScanCode];
        };
    }
}

/// 申请取消
/// - Parameter sender: sender description
- (void)actionWithApplyCancel:(id)sender {
//    [SVProgressHUD showInfoWithStatus:@"取消申请"];
    NSMutableDictionary *params = @{@"serviceModel":self.viewModel.serviceModel}.mutableCopy;
    if (self.popResultBlock) {
        params[@"block"] = self.popResultBlock;
    } else {
        @weakify(self);
        void(^block)(id data) = ^(id data) {
            @strongify(self);
//            [self doRefreshData];
        };
        params[@"block"] = self.popResultBlock;
    }
    
    UIViewController *vc = [[TYZRouter sharedInstance] getServiceCancelWorkOrderVC:params];
    [TYZJumpVCManager pushBaseVC:self pushVC:vc];
}

/// 工单详情
- (void)actionWithDetail {
    
    if (![[EDSAppDelegateTool sharedInstance] hasPermission:@(10010101) parentCode:@(100101)]) {
        [SVProgressHUD showInfoWithStatus:@"您没有查看详情的权限！"];
        return;
    }
    
    
    NSMutableDictionary *params = @{@"serviceModel":self.viewModel.serviceModel}.mutableCopy;
    @weakify(self);
    void(^block)(id data) = ^(id data) {
        @strongify(self);
//            [self doRefreshData];
    };
    params[@"block"] = block;
    
    UIViewController *vc = [[TYZRouter sharedInstance] getServiceOrderWaitVehDetailVC:params];
    [TYZJumpVCManager pushBaseVC:self pushVC:vc];
}

/// 扫码接车
- (void)actionWithScanCode {
//    [SVProgressHUD showInfoWithStatus:@"扫码接车"];
    
    @weakify(self);
    void(^block)(id data) = ^(id data) {
        @strongify(self);
        if (self.popResultBlock) {
            self.popResultBlock(nil);
        }
    };
    NSDictionary *params = @{
        @"scanTitle": @"扫码接车",
        @"tipInfo": @"将二维码或条形码对准扫码区域，即可自动扫码扫码配件可以确认配件的版本及适配车辆",
        @"serviceModel": self.viewModel.serviceModel,
        @"block":block
    };
    UIViewController *scanCodeVC = [[TYZRouter sharedInstance] getScanCodeMeetCarVC:params];
    [TYZJumpVCManager pushBaseVC:self pushVC:scanCodeVC];
}


@end
